package com.houtai.controller.dict;

import com.houtai.authority.Authority;
import com.houtai.authority.AuthorityType;
import com.houtai.bean.dto.dict.TsJzDictSonListDto;
import com.houtai.bean.vo.dict.DictListResult;
import com.houtai.service.dict.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@Api(value="省市区接口", tags={"省市区接口"})
@RequestMapping(value = {"api/v1"}, method = RequestMethod.POST)
public class DictContorller {
    @Autowired
    private DictService dictService;


    @Authority(value = AuthorityType.NoValidate)
    @ApiOperation(value="省列表", notes="省列表")
    @RequestMapping(value = "/province/list", method = RequestMethod.POST)
    public DictListResult province() {
        return dictService.findDictByType("province");
    }

    @ApiOperation(value="市列表", notes="市列表")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/city/list")
    public DictListResult list(@ApiParam(value = "市列表所需参数") @Valid @RequestBody TsJzDictSonListDto dto, BindingResult result) {
        DictListResult retInfo = new DictListResult();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = dictService.findDictByParentId(dto.getParent_id());
        }
        return retInfo;
    }
    @ApiOperation(value="区列表", notes="区列表")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/county/list")
    public DictListResult county(@ApiParam(value = "区列表所需参数") @Valid @RequestBody TsJzDictSonListDto dto, BindingResult result) {
        DictListResult retInfo = new DictListResult();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = dictService.findDictByParentId(dto.getParent_id());
        }
        return retInfo;
    }

}
